﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameWork.Source.Animation_Classes;
using FrameWork.Source.Texture;
using FrameWork.Source.Resource_Manager;
using Boomer.Source.PathFinding;
using Microsoft.Xna.Framework;
using FrameWork.Source.Utilities;

namespace Boomer.Source.Character
{
    class CMonster : CMob
    {
        public enum MOBTYPE
        {
            MUMMY = 0,
            BOSS = 1,
            BAT = 2
        }
        private bool isFixedPos;
        private MOBTYPE MobType;
        private CResourceManager.IMG_MAIN img;
        private float LocalTime;
        private int direction;
        public CMonster()
            : base()
        {
            this.Tag = FrameWork.Tag_Manager.TagManager.TAGGED.Enemy;
            isFixedPos = false;
            this.speed = 50.0f;
        }

        public void setMobType(MOBTYPE mobtype)
        {
            MobType = mobtype;
        }

        public override void init()
        {
            CAnimation[] pAnim;
            pAnim = new CAnimation[6];

            if (MobType == MOBTYPE.MUMMY)
            {
                pAnim[0] = new CAnimation();
                pAnim[0].createAnimation("Stand", 8, 8, 8, 1.0f);
                pAnim[1] = new CAnimation();
                pAnim[1].createAnimation("Stand Animation", 0, 8, 0, 1.0f / 10.0f);
                pAnim[2] = new CAnimation();
                pAnim[2].createAnimation("Move Left", 9, 14, 9, 1.0f / 10.0f);
                pAnim[3] = new CAnimation();
                pAnim[3].createAnimation("Move Right", 15, 20, 15, 1.0f / 10.0f);
                pAnim[4] = new CAnimation();
                pAnim[4].createAnimation("Move Top", 21, 26, 21, 1.0f / 10.0f);
                pAnim[5] = new CAnimation();
                pAnim[5].createAnimation("Move Bot", 27, 31, 27, 1.0f / 6.0f);


                img = CResourceManager.IMG_MAIN.MOBMUMMY;

                CTexture image = CResourceManager.getInstance().getImage((int)img);
                loadSprite(image, 32, 10, 56, 76, pAnim);
                createBoundBox(12.0f, 15.0f, 45, 30/*20.0f, 20.0f, 30, 25*/);
            }
            else
                if (MobType == MOBTYPE.BAT)
                {
                    pAnim[0] = new CAnimation();
                    pAnim[0].createAnimation("Stand", 17, 17, 17, 1.0f);
                    pAnim[1] = new CAnimation();
                    pAnim[1].createAnimation("Stand Animation", 0, 6, 0, 1.0f / 6.0f);
                    pAnim[2] = new CAnimation();
                    pAnim[2].createAnimation("Move Left", 7, 11, 7, 1.0f / 10.0f);
                    pAnim[3] = new CAnimation();
                    pAnim[3].createAnimation("Move Right", 12, 16, 12, 1.0f / 10.0f);
                    pAnim[4] = new CAnimation();
                    pAnim[4].createAnimation("Move Top", 17, 20, 17, 1.0f / 10.0f);
                    pAnim[5] = new CAnimation();
                    pAnim[5].createAnimation("Move Bot", 21, 25, 21, 1.0f / 6.0f);

                    img = CResourceManager.IMG_MAIN.MOBBAT;

                    CTexture image = CResourceManager.getInstance().getImage((int)img);
                    loadSprite(image, 26, 10, 56, 62, pAnim);
                    createBoundBox(9.0f, 9.0f, 35, 25);
                }
                else
                    if (MobType == MOBTYPE.BOSS)
                    {
                        pAnim[0] = new CAnimation();
                        pAnim[0].createAnimation("Stand", 15, 15, 15, 1.0f);
                        pAnim[1] = new CAnimation();
                        pAnim[1].createAnimation("Stand Animation", 15, 15, 15, 1.0f / 4.0f);
                        pAnim[2] = new CAnimation();
                        pAnim[2].createAnimation("Move Left", 0, 4, 0, 1.0f / 10.0f);
                        pAnim[3] = new CAnimation();
                        pAnim[3].createAnimation("Move Right", 5, 9, 5, 1.0f / 10.0f);
                        pAnim[4] = new CAnimation();
                        pAnim[4].createAnimation("Move Top", 10, 14, 10, 1.0f / 10.0f);
                        pAnim[5] = new CAnimation();
                        pAnim[5].createAnimation("Move Bot", 15, 19, 15, 1.0f / 6.0f);

                        img = CResourceManager.IMG_MAIN.BOSS;

                        CTexture image = CResourceManager.getInstance().getImage((int)img);
                        loadSprite(image, 32, 10, 56, 67, pAnim);
                        createBoundBox(9.0f, 20.0f, 35, 25);
                    }
        }

        public bool initDir()
        {
            int count = listCantMoveDir.Count;

            foreach (DIRECTION direct in listCantMoveDir)
            {
                if ((DIRECTION)direction == direct)
                    return true;
            }

            listCantMoveDir.Clear();
            return false;
        }

        public override void update(float deltaTime)
        {
            if (!isDying)
            {



                DIRECTION dir = DIRECTION.NONE;
                float speed = deltaTime * this.speed;
                LocalTime += deltaTime;
                if (direction == 0)
                {
                    this.changeAnimation("Stand");
                }
                if (isChangeDir)
                {
                    isChangeDir = false;
                    direction = CUtilities.getInstance().randomNumber(0, 4);
                    LocalTime = 0;
                }
                if (LocalTime >= 5.0f)
                {
                    direction = CUtilities.getInstance().randomNumber(0, 4);
                    LocalTime = 0;
                }
                else
                {
                    if (direction == 3)
                    {
                        dir = DIRECTION.BOT;
                        this.changeAnimation("Move Bot");
                    }
                    else if (direction == 2)
                    {
                        dir = DIRECTION.TOP;
                        this.changeAnimation("Move Top");
                        speed = -speed;
                    }
                    else if (direction == 1)
                    {
                        dir = DIRECTION.RIGHT;
                        this.changeAnimation("Move Right");
                    }
                    else if (direction == 0)
                    {
                        dir = DIRECTION.LEFT;
                        this.changeAnimation("Move Left");
                        speed = -speed;
                    }
                    move(dir, speed);
                }
            }
            else
            {
                this.changeAnimation("Stand");
                dying(deltaTime);
            }
            
            base.update(deltaTime);
        }

        public override void draw()
        {
            base.draw();
        }

        public override bool Collision(ref global::FrameWork.Source.Object_Classes.CObject pObj)
        {
            return base.Collision(ref pObj);
        }

        public override void move(DIRECTION dir, float speed)
        {
            base.move(dir, speed);
        }
    }
}
